package cn.itlaobing.springbootdemo.web.controller;

import cn.itlaobing.springbootdemo.Constants;
import cn.itlaobing.springbootdemo.entity.SystemAccount;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class BaseController {
   protected Log log= LogFactory.getLog(this.getClass());
   /**
    * 获取当前登录用户
    * @return
    */
   protected SystemAccount getCurrentSystemAccount(){
      //TODO 自行研究 ThreadLocal的用法
      //底层原理： 当请求到来的时候，SpringMVC将request对象放入到了 ThreadLocal， 下面的方法是在当前的线程的 ThreadLocal 中获取reqeust对象
      HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
      HttpSession session=request.getSession();
      return (SystemAccount)session.getAttribute(Constants.CURRENT_ACCOUNT);
   }

}
